package com.elong.android.specialhouse.base;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.ui.CustomDialogBuilder;
import com.elong.android.specialhouse.utils.Utils;

public class DialogUtils {
	
	/**
	 * 
	 * @Title: showConfirmDialogNoDail
	 * @Description: 显示确认对话框
	 * @param context
	 * @param title
	 * @param info
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void showConfirmDialogNoDail(Context context, String title,
			String info, DialogInterface.OnClickListener listener) {

		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return;
			}
		}

		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		builder.setTitle(title);
		builder.setMessage(info);
		builder.setPositiveButton(R.string.go_on, listener);
		builder.setCancelable(listener == null);
		builder.show();
	}

	/**
	 * 弹出对话框
	 * 
	 * @param context
	 *            对话框依附的activity
	 * @param title
	 *            标题
	 * @param info
	 *            描述
	 * @param confirmResId
	 *            描述确认按钮的string字符串对应的id
	 * @param cancelResId
	 *            描述取消按钮的string字符串对应的id
	 * @param listener
	 *            回调监听
	 */
	public final static CustomDialogBuilder showConfirmDialog(Context context,
			String title, String info, int confirmResId, int cancelResId,
			DialogInterface.OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return null;
			}
		}
		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(info);
		builder.setPositiveButton(confirmResId, listener);
		builder.setNegativeButton(cancelResId, listener);
		builder.setCancelable(false);
		builder.show();
		return builder;
	}

	/**
	 * @author Qizhi.Deng 弹出对话框带取消功能
	 * 
	 * @param context
	 *            对话框依附的activity
	 * @param title
	 *            标题
	 * @param info
	 *            描述
	 * @param confirmResId
	 *            描述确认按钮的string字符串对应的id
	 * @param cancelResId
	 *            描述取消按钮的string字符串对应的id
	 * @param listener
	 *            回调监听
	 * @param isCancelable
	 *            弹窗是否可以取消
	 */
	public final static CustomDialogBuilder showConfirmDialog(Context context,
			String title, String info, int confirmResId, int cancelResId,
			boolean isCancelable, DialogInterface.OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return null;
			}
		}
		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(info);
		builder.setPositiveButton(confirmResId, listener);
		builder.setNegativeButton(cancelResId, listener);
		builder.setCancelable(isCancelable);
		builder.show();
		return builder;
	}

	/**
	 * @author jinzhong.he 弹出对话框带取消功能
	 * 
	 * @param context
	 *            对话框依附的activity
	 * @param title
	 *            标题
	 * @param info
	 *            描述
	 * @param confirmResId
	 *            描述确认按钮的string字符串对应的id
	 * @param cancelResId
	 *            描述取消按钮的string字符串对应的id
	 * @param listener
	 *            回调监听
	 * @param isCancelable
	 *            弹窗是否可以取消
	 */
	public final static CustomDialogBuilder showConfirmDialog(Context context,
			String title, int info, int confirmResId, int cancelResId,
			boolean isCancelable, DialogInterface.OnClickListener listener) {
		return showConfirmDialog(context, title, context.getString(info),
				confirmResId, cancelResId, isCancelable, listener);
	}

	/**
	 * 
	 * @Title: showInfo
	 * @Description: 显示对话框
	 * @param context
	 * @param title
	 * @param info 自定义确定按钮意义的显示文案
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void showInfoWithCustomText(Context context, String title, String info,int resId,
			DialogInterface.OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return;
			}
		}

		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		builder.setTitle(title);
		builder.setMessage(info);
		builder.setPositiveButton(resId, listener);
		builder.setCancelable(true);
		builder.show();
	}
	/**
	 *
	 * @Title: showInfo
	 * @Description: 显示对话框
	 * @param context
	 * @param title
	 * @param info
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void showInfo(Context context, String title, String info,
			DialogInterface.OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return;
			}
		}

		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		builder.setTitle(title);
		builder.setMessage(info);
		builder.setPositiveButton(R.string.confirm,
				listener);
		builder.setCancelable(listener == null);
		builder.show();
	}

	/**
	 * 
	 * @Title: showInfo
	 * @Description: 显示对话框, 至于确认按钮,可以设置自定义的ContentView ,以及contentView的点击事件
	 * @param context
	 * @param title
	 * @param info
	 * @param resId
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void showInfo(Context context, String title, String info,
			int resId, OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return;
			}
		}

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View contentView = inflater.inflate(resId, null);
		contentView.setOnClickListener(listener);
		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		builder.setTitle(title);
		builder.setMessage(info);
		builder.setContentView(contentView);
		builder.setPositiveButton(R.string.goon_fillin,
				null);
		builder.setCancelable(listener == null);
		builder.show();
	}
	
	/**
	 * 
	 * @Title: showInfo
	 * @Description: 显示不可取消的，只有一个确定按钮的对话框
	 * @param context
	 * @param title
	 * @param info
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void showNoCancelInfo(Context context, String title,
			String info, DialogInterface.OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return;
			}
		}

		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		builder.setTitle(title);
		builder.setMessage(info);
		builder.setPositiveButton(R.string.confirm,
				listener);
		builder.setCancelable(false);
		builder.show();
	}

	/**
	 * 
	 * @Title: showInfo
	 * @Description: 显示对话框
	 * @param context
	 * @param titleid
	 * @param infoid
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void showInfo(Context context, int titleid, int infoid,
			DialogInterface.OnClickListener listener) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return;
			}
		}

		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		if (titleid != -1) {
			builder.setTitle(context.getString(titleid));
		}
		builder.setMessage(context.getString(infoid));
		builder.setPositiveButton(R.string.confirm,
				listener);
		builder.setCancelable(listener == null);
		builder.show();
	}

	/**
	 * 
	 * @Title: showInfo
	 * @Description: 显示对话框
	 * @param context
	 * @param titleId
	 * @param infoId
	 * @return void
	 * @throws
	 */
	public static void showInfo(Context context, int titleId, int infoId) {
		if (titleId != -1) {
			showInfo(context, context.getString(titleId),
					context.getString(infoId));
		} else {
			showInfo(context, null, context.getString(infoId));
		}
	}

	/**
	 * 
	 * @Title: showInfo
	 * @Description: 显示对话框
	 * @param context
	 * @param title
	 * @param info
	 * @return void
	 * @throws
	 */
	public static void showInfo(Context context, String title, String info) {
		showInfo(context, title, info, null);
	}

	/**
	 * 
	 * @Title: showConfirmDialog
	 * @Description: 显示确认对话框
	 * @param context
	 * @param title
	 * @param info
	 * @param listener
	 * @param args
	 * @return CustomDialogBuilder
	 * @throws
	 */
	public final static CustomDialogBuilder showConfirmDialog(Context context,
			String title, String info,
			DialogInterface.OnClickListener listener, Object... args) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return null;
			}
		}
		CustomDialogBuilder builder = new CustomDialogBuilder(context,
				CustomDialogBuilder.DIALOG_IDS, 1);
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(info);
		int confirmResId = R.string.confirm;
		int cancelResId = R.string.cancel;
		if (args.length > 0) {
			int mode = Utils.convertToInt(args[0], 0);
			if (mode == Utils.CONFIRMDIALOG_TYPE_YESNO) {
				confirmResId = R.string.yes;
				cancelResId = R.string.no;
			}
			if (mode == Utils.CONFIRMDIALOG_TYPE_YESDIAL) {
				confirmResId = R.string.go_on;
				cancelResId = R.string.dial;
			}
		}
		builder.setPositiveButton(confirmResId, listener);
		builder.setNegativeButton(cancelResId, listener);
		builder.setCancelable(false);
		builder.show();
		return builder;
	}
	
	/**
	 * 
	 * @Title: showNetworkUnavailableDialog
	 * @Description: 网络不用，打开系统网络设值界面
	 * @param
	 * @return void
	 * @throws
	 */
	public static void showNetworkUnavailableDialog(final Activity activity) {
		showConfirmDialog(activity, activity.getString(R.string.network_unavailable),
				activity.getString(R.string.network_unavailable_prompt), R.string.network_setting, R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						if (which == DialogInterface.BUTTON_POSITIVE) {
							Intent intent = null;
							if (android.os.Build.VERSION.SDK_INT > 10) {
								intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
							} else {
								intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
							}
							activity.startActivity(intent);
						}
					}
				});
	}
	
	/**
	 * 
	 * @Title: validatePwdSuccess
	 * @Description: 密码是否合法
	 * @param activity
	 * @param phoneNo
	 * @param email
	 * @param pwd
	 * @return boolean
	 * @throws
	 */
	public static boolean validatePwdSuccess(Activity activity, String phoneNo, String email, String pwd) {
		String errorMsg = null;
		switch (Utils.validatePwd(phoneNo, email, pwd)) {
		case Utils.VALIDATE_PWD_ERROR_NULL:
			errorMsg = activity.getString(R.string.password_error_null);
			break;
		case Utils.VALIDATE_PWD_ERROR_LENGTH:
			errorMsg = activity.getString(R.string.password_error_length);
			break;
		case Utils.VALIDATE_PWD_ERROR_SAME_PHONE:
			errorMsg = activity.getString(R.string.password_error_phone);
			break;
		case Utils.VALIDATE_PWD_ERROR_SAME_EMAIL:
			errorMsg = activity.getString(R.string.password_error_email);
			break;
		case Utils.VALIDATE_PWD_ERROR_SAME_SPECIAL:
			errorMsg = activity.getString(R.string.password_error_character);
			break;
		}
		if (errorMsg != null) {
			showInfo(activity, null, errorMsg);
			return false;
		}
		return true;
	}
	
	public static void showToast(Context context, int tipID, boolean isCenter) {
		Toast toast = Toast.makeText(context, tipID, Toast.LENGTH_LONG);
		if (isCenter) {
			toast.setGravity(Gravity.CENTER, 0, 0);
		}
		toast.show();
	}
	
	public static void showToast(Context context, String tip, boolean isCenter) {
		Toast toast = Toast.makeText(context, tip, Toast.LENGTH_LONG);
		if (isCenter) {
			toast.setGravity(Gravity.CENTER, 0, 0);
		}
		toast.show();
	}
	
	/**
	 * @author Qizhi.Deng 弹出对话框带取消功能
	 * 
	 * @param context
	 *            对话框依附的activity
	 * @param title
	 *            标题
	 * @param info
	 *            描述
	 * @param confirmResId
	 *            描述确认按钮的string字符串对应的id
	 * @param cancelResId
	 *            描述取消按钮的string字符串对应的id
	 * @param resId
	 *            自定义ContentView
	 * @param listener
	 *            回调监听
	 * @param isCancelable
	 *            弹窗是否可以取消
	 */
	public final static CustomDialogBuilder showConfirmDialog(Context context, String title, String info,
			  int cancelResId,int confirmResId,int resId, boolean isCancelable, DialogInterface.OnClickListener listener) {

		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) {
				return null;
			}
		}
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View contentView = inflater.inflate(resId, null);
		CustomDialogBuilder builder = new CustomDialogBuilder(context, CustomDialogBuilder.DIALOG_IDS, 1);
		if (title != null) {
			builder.setTitle(title);
		}
		builder.setMessage(info);
		builder.setContentView(contentView);
		builder.setPositiveButton(confirmResId, listener);
		builder.setNegativeButton(cancelResId, listener);
		builder.setCancelable(isCancelable);
		builder.show();
		return builder;
	}
}